/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tlf.dyjc5.listener;

import com.tlf.dyjc5.service.ConstantService;
import java.util.HashMap;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSession;
import org.dusin.task.AutoTaskThread;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * Web application lifecycle listener.
 *
 * @author tengyu
 */
public class ContextListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("context initialized");
        ServletContext sc = sce.getServletContext();
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());//获取应用程序的配置信息
        ConstantService constantService = (ConstantService) context.getBean("constantService");
        
        String currentOrgCode = constantService.getValue("当前单位编码", "01");
        sce.getServletContext().setAttribute("currentOrgCode", currentOrgCode);
        
        String rsjRateBase = constantService.getValue("日数据合格率基准", "01");
        sce.getServletContext().setAttribute("rsjRateBase", rsjRateBase);
        
        String ysjRateBase = constantService.getValue("月数据合格率基准", "01");
        sce.getServletContext().setAttribute("ysjRateBase", ysjRateBase);
        
        String isDailyChartShow = constantService.getValue("显示日合格率走势", "01");
        System.out.println(isDailyChartShow);
        if ("关|off".indexOf(isDailyChartShow.trim()) != -1) {
            sce.getServletContext().setAttribute("isDailyChartShow", false);
        } else {
            sce.getServletContext().setAttribute("isDailyChartShow", true);
        }
        
        String rsjAutoRecallTime = constantService.getValue("日数据自动补采时间", "01");
        sc.setAttribute("rsjAutoRecallTime", rsjAutoRecallTime);
        
        String ysjAutoRecallTime = constantService.getValue("月数据自动补采时间", "01");
        sc.setAttribute("ysjAutoRecallTime", ysjAutoRecallTime);
        
        String rsjAutoProcessTime = constantService.getValue("日数据自动处理时间", "01");
        sc.setAttribute("rsjAutoProcessTime", rsjAutoProcessTime);
        
        String ysjAutoProcessTime = constantService.getValue("月数据自动处理时间", "01");
        sc.setAttribute("ysjAutoProcessTime", ysjAutoProcessTime);
        
        String isRsjAdjust = constantService.getValue("日数据自动调整", "01");
        sc.setAttribute("isRsjAdjust", isRsjAdjust);
        
        String isYsjAdjust = constantService.getValue("月数据自动调整", "01");
        sc.setAttribute("isYsjAdjust", isYsjAdjust);
        
        String rsjAutoUploadTime = constantService.getValue("日数据自动上传时间", "01");
        sc.setAttribute("rsjAutoUploadTime", rsjAutoUploadTime);
        
        String ysjAutoUploadTime = constantService.getValue("月数据自动上传时间", "01");
        sc.setAttribute("ysjAutoUploadTime", ysjAutoUploadTime);
        
        String data5mAutoGenTime = constantService.getValue("5分钟数据生成时间", "01");
        sc.setAttribute("data5mAutoGenTime", data5mAutoGenTime);
        
        sce.getServletContext().setAttribute("userList", new HashMap<String, HttpSession>());
        sce.getServletContext().setAttribute("vipList", new HashMap<String, HttpSession>());

        AutoTaskThread taskThread = (AutoTaskThread) sce.getServletContext().getAttribute("autoTaskThread");
        if (taskThread == null) {
            taskThread = new AutoTaskThread(sce.getServletContext());
            sce.getServletContext().setAttribute("autoTaskThread", taskThread);
        }
        taskThread.start();
    }

    public void contextDestroyed(ServletContextEvent sce) {
        AutoTaskThread taskThread = (AutoTaskThread) sce.getServletContext().getAttribute("autoTaskThread");
        if (taskThread != null && taskThread.isAlive()) {
            //System.out.println("$$$$$$$$$$$$$$$$结束线程"+taskThread.getName());
            taskThread.makeItStop();
        }
        System.out.println("contextDestroyed");
    }
}
